 /*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package unb.wikinarua.geopartitura.view;

import unb.wikinarua.geopartitura.Menu;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class MenuButtonView extends View {

	private static final int IMG_HALF_SIZE = 28;
	private static final int MARGIN = 28;
	
	private Context context;
	
	private int image;
	private int type;
	
	private float drawX;
	private float drawY;
	private float x;
	private float y;
	
	private Paint paint = new Paint();
	private Bitmap b; 
	
	public MenuButtonView(Context context, int type, int image, float x, float y) {
		super(context);
		
		this.type = type;
		this.context = context;
		this.image = image;
		this.x = x;
		this.y = y;
		
		b = BitmapFactory.decodeResource(context.getResources(), image);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.save();
		
		float centerX = canvas.getWidth() / 2;
		float centerY = canvas.getHeight() / 2;
		
		drawX = centerX + x - IMG_HALF_SIZE;
		drawY = centerY + y - IMG_HALF_SIZE;
		
		canvas.translate(drawX, drawY);
		canvas.drawBitmap(b, 0, 0, paint);
		canvas.restore();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if ((event.getX() > drawX - IMG_HALF_SIZE + MARGIN) && (event.getX() < drawX + IMG_HALF_SIZE + MARGIN)
				&& (event.getY() > drawY - IMG_HALF_SIZE + MARGIN) && (event.getY() < drawY + IMG_HALF_SIZE + MARGIN)) {
			((Menu) context).handler.sendEmptyMessage(type);
		}
		return super.onTouchEvent(event);
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public int getImage() {
		return image;
	}

	public void setImage(int image) {
		this.image = image;
	}
	
}
